#encoding=utf8
'''
Created on Aug 13, 2015

@author: jingyang <jingyang@smalltreemedia.com>
'''
from django.shortcuts import render_to_response
from django.template.context import RequestContext
from qa_info.models import DevQAInfo, ReleaseQAInfo
from sub_info.models import SubInfo
from app_info.models import UserGroup, ProductionTeam, AppVersion, APP_STORE
from django.template.loader import get_template
from utils import JsonResponse


APP_STORE_LIST = [x[0] for x in APP_STORE]
A_TAG = "<a href='/admin/app_info/app/%s/#version' target='_blank'>%s (V%s)</a>"


def get_qa_kanban(req):
    ctx = {'title': 'QA Kanban Board', 'divisions': UserGroup.objects.all()}
    if not req.is_ajax():
        return render_to_response('base/qa_kanban_board.html',
            RequestContext(req, ctx))
    division = req.REQUEST.get('division', '').strip(',').split(',')
    team = req.REQUEST.get('team', '').strip(',').split(',')
    type = req.REQUEST.get('type', '').strip(',').split(',')

    base = AppVersion.objects.filter(status__in=['I', 'S'])
    if type[0]:
        base = base.filter(type__in=type)
    if division[0]:
        base = base.filter(prd_team__group__in=division)
    if team[0]:
        base = base.filter(prd_team__in=team)

    s_v = base.filter(status='S').values_list('app', 'app__prj_code', 'code')
    s_v_dict = {
        store: "<br />".join([A_TAG % v for v in s_v.filter(app__app_store=store)])\
        for store in APP_STORE_LIST}

    in_prd = base.filter(status='I')

    c_r_q, i_r_q, p_r_q, c_d_q, i_d_q, p_d_q = [], [], [], [], [], []
    for i in in_prd:
        release_qa_set = i.get_release_qa()
        dev_qa_set = i.get_dev_qa()
        if release_qa_set.filter(status='D').exists():
            c_r_q.append((i.app_id, i.app.prj_code, i.code, i.app.app_store))
            continue
        if release_qa_set.filter(status='I').exists():
            i_r_q.append((i.app_id, i.app.prj_code, i.code, i.app.app_store))
            continue
        if release_qa_set.filter(status='P').exists():
            p_r_q.append((i.app_id, i.app.prj_code, i.code, i.app.app_store))
            continue
        if dev_qa_set.filter(status='D').exists():
            c_d_q.append((i.app_id, i.app.prj_code, i.code, i.app.app_store))
            continue
        if dev_qa_set.filter(status='I').exists():
            i_d_q.append((i.app_id, i.app.prj_code, i.code, i.app.app_store))
            continue
        if dev_qa_set.filter(status='P').exists():
            p_d_q.append((i.app_id, i.app.prj_code, i.code, i.app.app_store))
            continue
    c_r_q_dict = {store: "<br />".join([A_TAG % (x[0], x[1], x[2]) for x in c_r_q if x[3] == store])\
             for store in APP_STORE_LIST}
    i_r_q_dict = {store: "<br />".join([A_TAG % (x[0], x[1], x[2]) for x in i_r_q if x[3] == store])\
             for store in APP_STORE_LIST}
    p_r_q_dict = {store: "<br />".join([A_TAG % (x[0], x[1], x[2]) for x in p_r_q if x[3] == store])\
             for store in APP_STORE_LIST}
    c_d_q_dict = {store: "<br />".join([A_TAG % (x[0], x[1], x[2]) for x in c_d_q if x[3] == store])\
             for store in APP_STORE_LIST}
    i_d_q_dict = {store: "<br />".join([A_TAG % (x[0], x[1], x[2]) for x in i_d_q if x[3] == store])\
             for store in APP_STORE_LIST}
    p_d_q_dict = {store: "<br />".join([A_TAG % (x[0], x[1], x[2]) for x in p_d_q if x[3] == store])\
             for store in APP_STORE_LIST}

    data = [{'k': store, 'v': [p_d_q_dict[store], i_d_q_dict[store], c_d_q_dict[store],
        p_r_q_dict[store], i_r_q_dict[store], c_r_q_dict[store], s_v_dict[store]]}\
        for store in APP_STORE_LIST]
    headers = [('Pending Dev QA', p_d_q), ('In Dev QA', i_d_q),
               ('Dev QA Cleared', c_d_q), ('Pending Release QA', p_r_q),
               ('In Release QA', i_r_q), ('Release QA Cleared', c_r_q),
               ('Submitted', s_v)]
    ctx.update({'data': data, 'headers': headers})
    t = get_template('base/kanban.html')
    html = t.render(RequestContext(req, ctx))
    return JsonResponse({'status': 1, 'html': html})
